package com.plugin.sproutcore.ui.launch;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Text;

import com.plugin.sproutcore.ui.widgets.ProjectBrowserController;

public class SproutCoreMainTab extends AbstractLaunchConfigurationTab 
{
	public static final String PROJECTPATH_ATTR = "projectPath";
	
	
	private final ProjectBrowserController projectBrowser;
	
	private Text containerText;
	
	public SproutCoreMainTab()
	{
		IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject();
		projectBrowser = new ProjectBrowserController(this, project);
	}

	public void createControl(Composite parent) 
	{
		final Composite container = new Composite(parent, SWT.NULL);
		container.setLayout(new GridLayout());

		createProjectBrowseComponent(container);
		
		setControl(container);
		refresh();
	}
	
	private void createProjectBrowseComponent(Composite parent)
	{
		final Group group = new Group(parent, SWT.SHADOW_ETCHED_IN);
		final GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		group.setLayout(layout);
		group.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		group.setText("Project target");

		final GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = layout.numColumns;
		
		containerText = new Text(group, SWT.BORDER | SWT.SINGLE);
		containerText.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		
		Button button = new Button(group, SWT.PUSH);
		button.setText("Browse...");
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				try {
					handleBrowse();
				} catch (CoreException ex) {
					ex.printStackTrace();
				}
			}
		});
	}
	
	private void handleBrowse() throws CoreException
	{
		projectBrowser.handleBrowse();
		refresh();
		updateLaunchConfigurationDialog();
	}
	
	private void refresh()
	{
		IResource resource = projectBrowser.getResource();
		if (resource != null && containerText != null) {
			IPath path = resource.getLocation();
			if (path != null) {
				containerText.setText(path.toOSString());
			}
		}
	}

	public String getName() {
		return "Main";
	}
	
	public void initializeFrom(ILaunchConfiguration configuration) 
	{
		IResource file = null;
		try 
		{
			String projectPath = configuration.getAttribute(PROJECTPATH_ATTR, "");
			IPath path =  Path.fromPortableString(projectPath);
			if (!path.isEmpty()) {
				file = ResourcesPlugin.getWorkspace().getRoot().getProject(path.lastSegment());
				// TODO Handle file's other than Projects
			}
		} 
		catch (CoreException e) {}
		projectBrowser.setResource(file);
		refresh();
	}

	public void performApply(ILaunchConfigurationWorkingCopy configuration) 
	{
		IResource resource = projectBrowser.getResource();
		if (resource != null) 
		{
			IPath path = resource.getFullPath();
			if (path != null) {
				String resourcePath = path.toPortableString();
				configuration.setAttribute(PROJECTPATH_ATTR, resourcePath);
			}
		}
	}

	public void setDefaults(ILaunchConfigurationWorkingCopy configuration) 
	{
		
	}


}
